const winston = require('winston');
require('winston-daily-rotate-file');
import { config } from '../config/config';

const myFormat = winston.format.printf(({ level, message, label, timestamp }) => {
    return `${timestamp} ${level}: ${message}`;
});

const options = {
    level: config.loglevel,
    transports: [
        // - Write all logs with level `error` (and below) to `error.log`.
        new winston.transports.DailyRotateFile({
            level: 'error',
            filename: 'logs/error.%DATE%.log',
            auditFile: 'logs/.error-audit.json',
            datePattern: 'YYYY-MM',
            maxSize: '20m',
            maxFiles: '10',
            format: winston.format.combine(
                winston.format.timestamp(),
                myFormat
            )
        }),

        // - Write all logs with level `info` (and below) to `combined.log`.
        new winston.transports.DailyRotateFile({
            filename: 'logs/combined.%DATE%.log',
            auditFile: 'logs/.combined-audit.json',
            datePattern: 'YYYY-MM-DD',
            maxSize: '20m',
            maxFiles: '30d',
            format: winston.format.combine(
                winston.format.timestamp(),
                myFormat
            )
        }),

        // - Write all logs with specified level to console.
        new winston.transports.Console({
            format: winston.format.combine(
                winston.format.colorize(),
                winston.format.simple()
            )
        })
    ]
};

export const logger = winston.createLogger(options);
